package hkr.se1.budgetapp;

import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ListFragment extends Fragment {
	private EntriesDataSource EDS;
	private Activity activity;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity = activity;
		MainActivity mainAct = (MainActivity) activity;
		this.EDS = mainAct.getEntriesDataSource();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		return inflater.inflate(R.layout.list_layout, container, false);
	}

	@Override
	public void onStart() {
		super.onStart();
		List<Entry> list = EDS.getTopEntries();
		LinearLayout listView = (LinearLayout) activity.findViewById(R.id.dbList);
		for (int i = 0; i < list.size(); i++) {
			listView.addView(getListItem(list.get(i).getDate(), list.get(i)
					.getNote(), list.get(i).getCategory(), ""+list.get(i)
					.getAmount()));
		}
	}

	private LinearLayout getListItem(String dateString, String noteString,
			String categoryString, String amountString) {
		LinearLayout linearLayout = new LinearLayout(activity);
		TextView date = new TextView(activity);
		TextView note = new TextView(activity);
		TextView category = new TextView(activity);
		TextView amount = new TextView(activity);

		date.setPadding(20, 20, 20, 20);
		note.setPadding(20, 20, 20, 20);
		category.setPadding(20, 20, 20, 20);
		amount.setPadding(20, 20, 20, 20);
		
		date.setText(dateString);
		note.setText(noteString);
		category.setText(categoryString);
		amount.setText(amountString);

		linearLayout.addView(date);
		linearLayout.addView(note);
		linearLayout.addView(category);
		linearLayout.addView(amount);

		return linearLayout;
	}

}
